package com.jian.java.controller;

import com.jian.java.entities.Dept;
import com.jian.java.service.DeptService;
import com.jian.java.service.DeptServiceImpl;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Jiange
 * @create 2021-05-30-15:36
 */
@RestController
public class DeptController {
    @Autowired
    private DeptServiceImpl deptService;

    @GetMapping("/dept/add")
    public boolean add(Dept dept){
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/getall")
    public List<Dept> getAll(){
        return deptService.findAll();
    }

    @HystrixCommand(fallbackMethod = "errorMethod")
    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") int id){
        Dept byId = deptService.findById(id);
        if(null == byId){
            throw new RuntimeException("没有这个id"+id);
        }
        return byId;
    }
    public Dept errorMethod(@PathVariable("id") int id){
        return new Dept(0L,"没有这个","没有这个数据库");
    }

}
